<template>
    <div>
        <el-dialog title="上传" v-model="isShowDialog" width="600px" @close="closeDialog">
            <el-form ref="formRef" :model="formModel" :rules="formRules" size="default" label-width="120px">
                <el-form-item label="所属平台ID" prop="platformId">
                    <el-input v-model="formModel.platformId" show-word-limit maxlength="50" placeholder="" clearable
                        @change="onChangePlatform"></el-input>
                </el-form-item>
                <el-form-item label="图片" prop="platformId">
                    <UploadImage v-if="showUpload" :payload="payload" :disabled="disableUpload"></UploadImage>
                </el-form-item>
            </el-form>
            <template #footer>
                <span class="dialog-footer">
                    <el-button @click="() => closeDialog()" size="default">关 闭</el-button>
                </span>
            </template>
        </el-dialog>
    </div>
</template>

<script lang="ts">
import { reactive, toRefs, defineComponent, ref, nextTick } from 'vue';
import type { FormInstance } from 'element-plus';
import { FileInfoAddParamModel } from '/@/interfaces/JdfsystemModel';
import UploadImage from '/@/views/file-info/component/upload-image.vue';
import { handleAdd, handleValidateForm } from '../file-info-wrap';

interface State {
    isShowDialog: boolean | true,
    submitLoading: boolean | false,
    formModel: FileInfoAddParamModel,
    payload: any,
    formRules: any,
    disableUpload: boolean | true,
    showUpload: boolean | true,
}

export default defineComponent({
    name: 'FileInfoAdd',
    components: { UploadImage },
    setup(props, context) {
        const state = reactive<State>({
            isShowDialog: true,
            submitLoading: false,
            formModel: {
                platformId: undefined,
            },
            payload: undefined,
            formRules: {
                platformId: [{ required: true, message: '所属平台ID不能为空', trigger: 'change' }],
            },
            disableUpload: true,
            showUpload: true,
        });
        const methodMap = reactive({
            // 关闭弹窗
            closeDialog: function () {
                state.isShowDialog = false;
                context.emit('onClose');
            },
            // 新增
            onSubmit: async function () {
                setTimeout(() => state.submitLoading = false, 1500);
                // 表单验证
                let validate = await handleValidateForm(formRef.value);
                if (!validate) {
                    state.submitLoading = false;
                    return;
                }
                // 提交
                await handleAdd(state.formModel, methodMap.closeDialog);
            },
            onChangePlatform: function (value: string) {
                state.disableUpload = [undefined, ''].includes(value);
                state.payload = { platformId: value }
                if (!state.disableUpload) {
                    state.showUpload = false;
                    nextTick(() => state.showUpload = true);
                }
            },
        });
        const formRef = ref<FormInstance>();

        return {

            formRef,
            ...toRefs(state),
            ...toRefs(methodMap),
        };
    },
});
</script>
